import sys
import os
import nuke
"""
该脚本是解析nk工程的后台脚本，读取nk工程里的包括Read,Write,Camera以及lut阶段信息
"""


def read_info(nk_path,save_dir):
    nuke.scriptReadFile(nk_path)
    nuke_name=os.path.split(nk_path)[1] #'test.nk'
    nuke_dir_name=os.path.splitext(nuke_name)[0] #project name  test
    dir_path=os.path.join(save_dir,nuke_dir_name)  # E:/AA/test
    save_path=os.path.join(save_dir,nuke_dir_name,nuke_name)  # E:/AA/test/test.nk
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    data = {}
    sn = nuke.allNodes()
    for n in sn:
        #n.Class()
        name = n.name()
        if n.Class() in ['Camera2', 'ReadGeo2', 'Read', 'Write']:  #
            file = n['file'].value()
            file_name=os.path.split(file)[1]
            data[name] = [n.Class(), file]
            new_path=os.path.join('footage',n.Class(),name,file_name)  # footage/Read/read1/aa.text
            n['file'].setValue(new_path.replace('\\','/'))
        elif n.Class() == 'Vectorfield':
            file = n['vfield_file'].value()
            file_name=os.path.split(file)[1]
            data[name] = [n.Class(), file]
            new_path=os.path.join('footage',n.Class(),name,file_name)
            n['vfield_file'].setValue(new_path.replace('\\','/'))
    rt=nuke.Root()
    rt['project_directory'].setValue(dir_path.replace('\\','/'))
    nuke.scriptSave(save_path)
    project_data={'project':nuke_dir_name,'data':data}
    print('data:', project_data)




if __name__ == '__main__':
    read_info(sys.argv[1],sys.argv[2])